package com.ss.android.l;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Pair;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class a {
    private static int a;
    private static int b;
    private static final Pattern c;

    static {
        a = -1;
        b = -1;
        c = Pattern.compile("^0-([\\d]+)$");
    }

    public static int a() {
        int v0;
        if (a > 0) {
            v0 = a;
        } else {
            v0 = a("/sys/devices/system/cpu/possible");
            if (v0 < 0) {
                v0 = a("/sys/devices/system/cpu/present");
            }

            if (v0 < 0) {
                v0 = c("/sys/devices/system/cpu/");
            }

            if (v0 < 0) {
                v0 = Runtime.getRuntime().availableProcessors();
            }

            a = v0;
        }

        return v0;
    }

    public static Pair a(Context arg4) {
        Object v0 = arg4.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo v1 = new ActivityManager.MemoryInfo();
        ((ActivityManager) v0).getMemoryInfo(v1);
        return Pair.create(Long.valueOf(v1.availMem), Boolean.valueOf(v1.lowMemory));
    }

    private static int a(String arg5) {
        int v0_3;
        BufferedReader v1_1;
        BufferedReader v0 = null;
        try {
            v1_1 = new BufferedReader(new InputStreamReader(new FileInputStream(arg5)));
            String v0_2 = v1_1.readLine();
            if (v0_2 == null) {
                try {
                    v1_1.close();
                } catch (IOException v0_4) {
                }
                return -1;
            }
            v0_3 = b(v0_2);
            try {
                v1_1.close();
            } catch (IOException v1_2) {
            }
            return v0_3;
        } catch (Throwable v0_1) {
            if (v0 == null) {
                return -1;
            }
            try {
                v0.close();
            } catch (IOException v0_4) {

            }
            return -1;
        }
    }

    public static int b() {
        int v2_4;
        BufferedReader v1_1;
        int v0;
        int v3 = -1;
        if (b > 0) {
            v0 = b;
            return v0;
        }

        int v5 = a();
        int v4 = 0;
        v0 = v3;
        while (v4 < v5) {
            String v6 = "/sys/devices/system/cpu/cpu" + v4 + "/cpufreq/cpuinfo_max_freq";
            BufferedReader v2 = null;
            try {
                v1_1 = new BufferedReader(new InputStreamReader(new FileInputStream(v6)));
                String v2_2 = v1_1.readLine();
                if (v2_2 == null) {

                } else {
                    try {
                        v2_4 = Integer.parseInt(v2_2);
                    } catch (NumberFormatException v2_3) {
                        v2_4 = v3;
                    }
                    v0 = Math.max(v0, v2_4);
                }
                try {
                    v1_1.close();
                } catch (IOException v1_2) {
                }

                ++v4;
                continue;
            } catch (Throwable v0_1) {
                if (v2 != null) {
                    try {
                        v2.close();
                    } catch (IOException v1_2) {
                    }
                }
                ++v4;
                continue;
            }
        }
        b = v0;
        return v0;
    }

    private static int b(String arg3) {
        int v0 = -1;
        Matcher v1 = c.matcher(((CharSequence) arg3));
        if (v1.matches()) {
            String v1_1 = v1.group(1);
            try {
                v0 = Integer.parseInt(v1_1) + 1;
            } catch (NumberFormatException v1_2) {
            }
        }

        return v0;
    }

    private static int c(String arg2) {
        try {
            File[] v0_1 = new File(arg2).listFiles(new b());
            if (v0_1 == null) {
                return -1;
            }

            if (v0_1.length <= 0) {
                return -1;
            }

            int v0_2 = v0_1.length;
            return v0_2;
        } catch (Throwable v0) {
        }

        return -1;
    }
}

